package com.lemox.main;

import java.io.FileWriter;
import java.io.IOException;

public class OIOValue
{
	public static int       k;
	public static int       n;
	public static boolean[] used;
	public static int[][]   data;
	public static int       data_index;
	public static int[][]   result;
	public static int[]     min_value;
	public static int       min_value_index;
	
	public static void initValues ()
	{
		n = OIOMath.pow_2(k);
		used = new boolean[n];
		data = new int[n][k];
		min_value = new int[n];
		
		for (int i = 0; i < n; i++)
		{
			used[i] = false;
		}
		
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < k; j++)
			{
				data[i][j] = 0;
			}
		}
	}
	
	
	public static void setValue_Data_MinValueIndex ()
	{
		data_index = -1;
		min_value_index = -1;
		for (int union_index = 1; union_index < n; union_index += 2)
		{
			if (used[union_index % (n-1)] == false)
			{
				data_index++;
				min_value_index++;
				min_value[min_value_index] = (union_index % (n-1));
				if (min_value[min_value_index] == 0)min_value[min_value_index] = (n-1);
				// ---- Hey!! Give you a flag, judge the length of data[data_index][...]
				boolean been_used = false;
				for (int i = 0;i <= (k-1); i++)
				{
					int tmp;
					tmp = union_index * OIOMath.pow_2 (i);
					tmp %= (n-1);
					while (tmp < 0)
					{
						tmp += (n-1);
					}
					data[data_index][i] = tmp;
					if (tmp == 0) tmp = (n-1);
					if (used[tmp] == true)
					{
						been_used = true;
					}
					used[tmp] = true;
				}
				// --- if has been_used = true, delete this data[data_index][...]
				// --- and roll back
				if (been_used == true)
				{
					for (int i = 0; i <= (k-1); i++)
					{
						data[data_index][i] = 0;
					}
					data_index--;
				}
			}
		}
		
		
		System.out.println(data_index + 1 + " " + min_value_index + 1);
		
	}
	
	public static void writeDataFile () throws IOException
	{
		String data_file_name = Integer.toString(k) + ".txt";
		FileWriter dataFileWriter = new FileWriter(data_file_name);
		for (int i = 0; i <= data_index; i++)
		{
			for (int j = 0; j < k; j++)
			{
				dataFileWriter.write(Integer.toString(data[i][j]) + " ");
			}
			dataFileWriter.write("\n");
		}
		dataFileWriter.close();
	}
	
}
